package com.example.spring.demo.handler;

import com.example.spring.demo.code.UserType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author liangchao
 * 11/18/18 4:28 PM
 */
public class UserTypeHandler implements TypeHandler<UserType> {
    @Override
    public void setParameter(PreparedStatement ps, int i, UserType parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getCode());
    }

    @Override
    public UserType getResult(ResultSet rs, String columnName) throws SQLException {
        int i = rs.getInt(columnName);
        for (UserType type : UserType.values()) {
            if (type.getCode() == i) {
                return type;
            }
        }
        return null;
    }

    @Override
    public UserType getResult(ResultSet rs, int columnIndex) throws SQLException {
        int i = rs.getInt(columnIndex);
        for (UserType type : UserType.values()) {
            if (type.getCode() == i) {
                return type;
            }
        }
        return null;
    }
    @Override
    public UserType getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
